import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

const routes = [
  {
    path: "/",
    name: "",
    component: () => import("@/views/login.vue"),
    hidden: true,
    meta: {
      requireAuth: false
    }
  },
  {
    path: "/login",
    name: "登录",
    component: () => import("@/views/login.vue"),
    hidden: true,
    meta: {
      requireAuth: false
    }
  },

  {
    path: "/index",
    name: "首页",
    component: () => import("../views/layout.vue"),
    iconCls: "el-icon-tickets",
    children: [
      {
        path: "/gencode",
        name: "代码生成",
        component: () => import("../views/sys/gencode.vue"),
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/socketio",
        name: "socket在线",
        component: () => import("../views/sys/socketio.vue"),
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/home2",
        name: "测试2",
        component: () => import("../views/home/home2.vue"),
        meta: {
          requireAuth: false
        }
      }
    ]
  }
];

const router = new VueRouter({
  routes
});

// router.beforeEach((to, from, next) => {
//   if (to.meta.requireAuth) {
//     // 判断该路由是否需要登录权限
//     if (Vue.$store.state.token) {
//       // 通过vuex state获取当前的token是否存在
//       next();
//     } else {
//       next({
//         path: "/login",
//         query: { redirect: to.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
//       });
//     }
//   } else {
//     next();
//   }
// });

export default router;
